package com.xmy.cultivate.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xmy.cultivate.entity.*;
import com.xmy.cultivate.entity.views.GradeCustomRuleYearClass;
import com.xmy.cultivate.mapper.CourseSchedulingDetailMapper;
import com.xmy.cultivate.mapper.GradeCustomSetMapper;
import com.xmy.cultivate.mapper.GradeMapper;
import com.xmy.cultivate.service.*;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xmy.cultivate.util.IdWorker;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

import static org.apache.commons.lang3.BooleanUtils.or;

/**
 * <p>
 * 班级自定义设置 服务实现类
 * </p>
 *
 * @author hpiggy
 * @since 2025-03-28
 */
@Service
public class GradeCustomSetServiceImpl extends ServiceImpl<GradeCustomSetMapper, GradeCustomSet> implements IGradeCustomSetService {

    @Autowired
    @Lazy
    IGradeCustomSetService iGradeCustomSetService;
    @Autowired
    @Lazy
    IGradeCustomGradeService iGradeCustomGradeService;
    @Autowired
    @Lazy
    IGradeCustomRuleCountService iGradeCustomRuleCountService;
    @Autowired
    @Lazy
    IGradeCustomRuleTimesService iGradeCustomRuleTimesService;
    @Autowired
    @Lazy
    IGradeCustomRuleWeeksService iGradeCustomRuleWeeksService;
    @Autowired
    IMqPushService iMqPushService;

    @Autowired
    @Lazy
    IdWorker idWorker;

    @Autowired
    GradeCustomSetMapper gradeCustomSetMapper;

    @Autowired
    IGradeService iGradeService;

    @Autowired
    GradeMapper gradeMapper;

    @Autowired
    CourseSchedulingDetailMapper courseSchedulingDetailMapper;




    @Override
    public List<String> getStudentGradeName(
            String yearPart,
            String quarterNum,
            String lessonType,
            String schoolId,
            String subjectsId,
            String weekDates,
            String timeStart,
            String timeEnd,
            String schedulingType
    ){
        return gradeCustomSetMapper.getStudentGradeName(yearPart,quarterNum,lessonType,schoolId,subjectsId,weekDates, timeStart,  timeEnd , schedulingType);
    }





    @Override
    @Transactional
    public boolean saveGradeCustomSet(GradeCustomSet gradeCustomSet) {

        try {
            QueryWrapper<GradeCustomSet>  queryWrapper= new QueryWrapper<>();
            queryWrapper.eq("school_id", gradeCustomSet.getSchoolId());
            queryWrapper.eq("lesson_type",gradeCustomSet.getLessonType());
            queryWrapper.eq("year_part", gradeCustomSet.getYearPart());
            queryWrapper.eq("quarter_num", gradeCustomSet.getQuarterNum());
            queryWrapper.eq("subjects_id", gradeCustomSet.getSubjectsId());
            queryWrapper.eq("deleted", 0);
            queryWrapper.last("limit 1");
            GradeCustomSet existingGradeCustomSet = getOne(queryWrapper);
            if (existingGradeCustomSet != null) {
                gradeCustomSet.setId(existingGradeCustomSet.getId());
                iGradeCustomSetService.updateById(gradeCustomSet);
                //修改时，先删除再保存
                iGradeCustomGradeService.remove(new QueryWrapper<GradeCustomGrade>().eq("grade_custom_set_id", gradeCustomSet.getId()));
                iGradeCustomRuleCountService.remove(new QueryWrapper<GradeCustomRuleCount>().eq("grade_custom_set_id", gradeCustomSet.getId()));
                iGradeCustomRuleTimesService.remove(new QueryWrapper<GradeCustomRuleTimes>().eq("grade_custom_set_id", gradeCustomSet.getId()));
                iGradeCustomRuleWeeksService.remove(new QueryWrapper<GradeCustomRuleWeeks>().eq("grade_custom_set_id", gradeCustomSet.getId()));
                if (gradeCustomSet.getGradeCustomGradeList() != null && !gradeCustomSet.getGradeCustomGradeList().isEmpty()) {
                    for (GradeCustomGrade gradeCustomGrade : gradeCustomSet.getGradeCustomGradeList()) {
                        gradeCustomGrade.setCreatedId(gradeCustomSet.getCreatedId());
                        gradeCustomGrade.setGradeCustomSetId(gradeCustomSet.getId());
                    }
                    //保存
                    iGradeCustomGradeService.saveBatch(gradeCustomSet.getGradeCustomGradeList());
                    //删除排课详情表
                    courseSchedulingDetailMapper.deleteCourseSchedulingDetail(gradeCustomSet.getGradeCustomGradeList(), gradeCustomSet.getYearPart(), gradeCustomSet.getQuarterNum(), gradeCustomSet.getSubjectsId(), gradeCustomSet.getLessonType(), gradeCustomSet.getSchoolId());
                    //删除自动排班
                    gradeMapper.deleteGrade(gradeCustomSet.getGradeCustomGradeList(), gradeCustomSet.getYearPart(), gradeCustomSet.getQuarterNum(), gradeCustomSet.getSubjectsId(), gradeCustomSet.getLessonType(), gradeCustomSet.getSchoolId());

                }else {
                    //删除全部排课详情表
                    courseSchedulingDetailMapper.deleteAllCourseSchedulingDetail(gradeCustomSet.getYearPart(), gradeCustomSet.getQuarterNum(), gradeCustomSet.getSubjectsId(), gradeCustomSet.getLessonType(), gradeCustomSet.getSchoolId());

                    //删除全部自动排班
                    QueryWrapper<Grade> gradeQueryWrapper = new QueryWrapper<>();
                    gradeQueryWrapper.eq("deleted", 0);
                    gradeQueryWrapper.eq("school_id", gradeCustomSet.getSchoolId());
                    gradeQueryWrapper.eq("quarter_num", gradeCustomSet.getQuarterNum());
                    gradeQueryWrapper.eq("year_part", gradeCustomSet.getYearPart());
                    gradeQueryWrapper.eq("lesson_type", gradeCustomSet.getLessonType());
                    gradeQueryWrapper.eq("subjects_id", gradeCustomSet.getSubjectsId());
                    gradeQueryWrapper.eq("add_type", 2);
                    gradeQueryWrapper.eq("scheduling_type","B类")
                    .or();
                    gradeQueryWrapper.eq("scheduling_type","A类") ;
                    iGradeService.remove(gradeQueryWrapper);
                }

                // 自定义排班规则
                if (gradeCustomSet.getCustomRuleYearClassList() != null && !gradeCustomSet.getCustomRuleYearClassList().isEmpty()) {
                    for (GradeCustomRuleYearClass gradeCustomRuleYearClass : gradeCustomSet.getCustomRuleYearClassList()) {
                        Integer yearClassId = gradeCustomRuleYearClass.getYearClassId();

                        // 自定义排班规则次数
                        if (gradeCustomRuleYearClass.getGradeCustomRuleCountList() != null && !gradeCustomRuleYearClass.getGradeCustomRuleCountList().isEmpty()) {
                            for (GradeCustomRuleCount gradeCustomRuleCount : gradeCustomRuleYearClass.getGradeCustomRuleCountList()) {
                                gradeCustomRuleCount.setCreatedId(gradeCustomSet.getCreatedId());
                                gradeCustomRuleCount.setGradeCustomSetId(gradeCustomSet.getId());
                                gradeCustomRuleCount.setYearClassId(yearClassId);
                            }
                            //保存
                            iGradeCustomRuleCountService.saveBatch(gradeCustomRuleYearClass.getGradeCustomRuleCountList());
                        }

                        // 自定义排班时间段
                        if (gradeCustomRuleYearClass.getGradeCustomRuleTimesList() != null && !gradeCustomRuleYearClass.getGradeCustomRuleTimesList().isEmpty()) {
                            for (GradeCustomRuleTimes gradeCustomRuleTimes : gradeCustomRuleYearClass.getGradeCustomRuleTimesList()) {
                                gradeCustomRuleTimes.setCreatedId(gradeCustomSet.getCreatedId());
                                gradeCustomRuleTimes.setGradeCustomSetId(gradeCustomSet.getId());
                                gradeCustomRuleTimes.setYearClassId(yearClassId);
                            }
                            //保存
                            iGradeCustomRuleTimesService.saveBatch(gradeCustomRuleYearClass.getGradeCustomRuleTimesList());
                        }

                        // 自定义排班星期
                        if (gradeCustomRuleYearClass.getGradeCustomRuleWeeksList() != null && !gradeCustomRuleYearClass.getGradeCustomRuleWeeksList().isEmpty()) {
                            for (GradeCustomRuleWeeks gradeCustomRuleWeeks : gradeCustomRuleYearClass.getGradeCustomRuleWeeksList()) {
                                gradeCustomRuleWeeks.setCreatedId(gradeCustomSet.getCreatedId());
                                gradeCustomRuleWeeks.setGradeCustomSetId(gradeCustomSet.getId());
                                gradeCustomRuleWeeks.setYearClassId(yearClassId);
                            }
                            //保存
                            iGradeCustomRuleWeeksService.saveBatch(gradeCustomRuleYearClass.getGradeCustomRuleWeeksList());
                        }
                    }
                }

            } else {
                gradeCustomSet.setId(idWorker.nextId());
                iGradeCustomSetService.save(gradeCustomSet);

            //班级自定义设置表
            if (gradeCustomSet.getGradeCustomGradeList() != null && !gradeCustomSet.getGradeCustomGradeList().isEmpty()) {
                for (GradeCustomGrade gradeCustomGrade : gradeCustomSet.getGradeCustomGradeList()) {
                    gradeCustomGrade.setCreatedId(gradeCustomSet.getCreatedId());
                    gradeCustomGrade.setGradeCustomSetId(gradeCustomSet.getId());
                }
                iGradeCustomGradeService.saveBatch(gradeCustomSet.getGradeCustomGradeList());
            }
            if (gradeCustomSet.getCustomRuleYearClassList() != null && !gradeCustomSet.getCustomRuleYearClassList().isEmpty()) {
                    for (GradeCustomRuleYearClass gradeCustomRuleYearClass : gradeCustomSet.getCustomRuleYearClassList()) {
                        Integer yearClassId = gradeCustomRuleYearClass.getYearClassId();
                        //自定义排班规则次数
                        if (gradeCustomRuleYearClass.getGradeCustomRuleCountList() != null && !gradeCustomRuleYearClass.getGradeCustomRuleCountList().isEmpty()) {
                            for (GradeCustomRuleCount gradeCustomRuleCount : gradeCustomRuleYearClass.getGradeCustomRuleCountList()) {
                                gradeCustomRuleCount.setCreatedId(gradeCustomSet.getCreatedId());
                                gradeCustomRuleCount.setGradeCustomSetId(gradeCustomSet.getId());
                                gradeCustomRuleCount.setYearClassId(yearClassId);
                            }
                            iGradeCustomRuleCountService.saveBatch(gradeCustomRuleYearClass.getGradeCustomRuleCountList());
                        }
                        //自定义排班时间段
                        if (gradeCustomRuleYearClass.getGradeCustomRuleTimesList() != null && !gradeCustomRuleYearClass.getGradeCustomRuleTimesList().isEmpty()) {
                            for (GradeCustomRuleTimes gradeCustomRuleTimes : gradeCustomRuleYearClass.getGradeCustomRuleTimesList()) {
                                gradeCustomRuleTimes.setCreatedId(gradeCustomSet.getCreatedId());
                                gradeCustomRuleTimes.setGradeCustomSetId(gradeCustomSet.getId());
                                gradeCustomRuleTimes.setYearClassId(yearClassId);
                            }
                            iGradeCustomRuleTimesService.saveBatch(gradeCustomRuleYearClass.getGradeCustomRuleTimesList());
                        }
                        //自定义排班星期
                        if (gradeCustomRuleYearClass.getGradeCustomRuleWeeksList() != null && !gradeCustomRuleYearClass.getGradeCustomRuleWeeksList().isEmpty()) {
                            for (GradeCustomRuleWeeks gradeCustomRuleWeeks : gradeCustomRuleYearClass.getGradeCustomRuleWeeksList()) {
                                gradeCustomRuleWeeks.setCreatedId(gradeCustomSet.getCreatedId());
                                gradeCustomRuleWeeks.setGradeCustomSetId(gradeCustomSet.getId());
                                gradeCustomRuleWeeks.setYearClassId(yearClassId);
                            }
                            iGradeCustomRuleWeeksService.saveBatch(gradeCustomRuleYearClass.getGradeCustomRuleWeeksList());
                        }

                    }
                }
//                iMqPushService.gradeCustomCreate(gradeCustomSet.getId());
            }
            iMqPushService.gradeCustomCreate(gradeCustomSet.getId(),gradeCustomSet.getCreatedId());
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

}
